package com.czy.ekp.gen;

import cn.hutool.core.io.resource.ResourceUtil;
import com.czy.ekp.gen.html.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.File;
import java.io.IOException;
import java.util.*;

public class GeneratorFactory {

    public static List<String> tempList = new ArrayList<>(Arrays.asList("create.sql", "list.html", "edit.html", "view.html"));
    static final Map<String, Class> tempMap = new HashMap();
    static Configuration conf = new Configuration();

    static {
        try {
            //root + File.separator + "temp"
            //模板目录
            conf.setDirectoryForTemplateLoading(new File(ResourceUtil.getResource("./").getFile() + "temp"));
        } catch ( IOException e) {
            e.printStackTrace();
        }

        tempMap.put("create.sql", SqlTemplateGenerator.class);
        tempMap.put("report.sql", SqlTemplateGenerator.class);
        tempMap.put("list.html", ListTemplateGenerator.class);
        tempMap.put("edit.html", EditTemplateGenerator.class);
        tempMap.put("view.html", ViewTemplateGenerator.class);
    }


    public static TemplateCodeGenerator newGenerator(String templateFile){
        if(!tempMap.containsKey(templateFile)){
            return null;
        }

        try{
            Template template = conf.getTemplate("/" + templateFile);
            return (TemplateCodeGenerator)tempMap.get(templateFile).getConstructor(Template.class).newInstance(template);
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }




}
